package org.dlsu.mt.parser;
import java.util.HashSet;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

public class FStructureSemantics implements FStructureInterface
{
	protected HashSet values;
	protected String name;

	public FStructureSemantics()
	{
		values = new HashSet();
		name = "*Sem*";
	}

	public void addElement(SemEntry sem)
	{
		String match = sem.getAttribute() + '\t' + sem.getValue();
		HashSet temp = new HashSet();
		 	Iterator i = values.iterator();
		 	while(i.hasNext())
		 	{
		 		FStructureElementValue fv = (FStructureElementValue)i.next();
				StringTokenizer tokens = new StringTokenizer(fv.toString(), "\t");
				if(!tokens.nextToken().equals(sem.getAttribute()))
					temp.add(fv);
		 	}
		 	if(match!= null)
		 		temp.add(new FStructureElementString(match));
		 	values = temp;
	}

	public HashSet getValues() {
		return values;
	}

	public boolean compareSemEntry(SemEntry sem)
	{
		String att = sem.getAttribute();
	 	Iterator i = values.iterator();
	 	while(i.hasNext())
	 	{
	 		FStructureElementValue fv = (FStructureElementValue)i.next();
			StringTokenizer tokens = new StringTokenizer(fv.toString(), "\t");
			if(tokens.nextToken().equals(att))
			{
				if(tokens.nextToken().equals(sem.getValue()))
					return true;
				else
					return false;
			}
	 	}
	 	return false;
	}

	public boolean containsSem(String att)
	{
		Vector<SemEntry> sems = getSemEntries();
		int i=0;
		while(i<sems.size())
		{
			if(sems.get(i).getAttribute().equals(att))
				return true;
			i++;
		}

		return false;
	}

	public Vector<SemEntry> getSemEntries()
	{
		Vector<SemEntry> list = new Vector();
		Iterator i = values.iterator();
		while(i.hasNext())
		{
			FStructureElementValue f = (FStructureElementValue)i.next();
			String value = f.toString();
			StringTokenizer tokens = new StringTokenizer(value, "\t");
			String temp = tokens.nextToken();
			String att = null;
			String[] target = null;
			StringTokenizer chop = new StringTokenizer(temp, " ");
			if(chop.countTokens() == 1)
				att = chop.nextToken();
			else
			{
				System.out.println("target found");
				target = new String[chop.countTokens()-1];
				int x=0;
				while(chop.hasMoreTokens())
				{
					String tok = chop.nextToken();
					if(!chop.hasMoreTokens())
					{
						att = tok;
					}
					else
					{
						target[x] = tok;
					}
					x++;
				}
			}
			String val = tokens.nextToken();
			if(!att.equals("FUNC")&&!att.equals("PRED")&&!att.equals("SEM"))
				if(target != null)
					list.add(new SemEntry(target, att, val));
				else
					list.add(new SemEntry(att, val));
		}

		if(!list.isEmpty())
			return list;
		return null;
	}


	 /**
	  * Function added by Marlon Tong
	  */
	  public void removeSem()
	  {
	  	HashSet temp = new HashSet();
	  	Iterator i = values.iterator();
	  	while(i.hasNext())
	  	{
	  		FStructureElementValue fv = (FStructureElementValue)i.next();
	  		if(!fv.toString().contains("SEM"))
	  			temp.add(fv);
	  	}
	  	values = temp;
	  }

	  public void assignSemEntry(SemEntry sem)
	  {
	  		String match = sem.getAttribute() + '\t' + sem.getValue();
	  		HashSet temp = new HashSet();
		 	Iterator i = values.iterator();
		 	while(i.hasNext())
		 	{
		 		FStructureElementValue fv = (FStructureElementValue)i.next();
				StringTokenizer tokens = new StringTokenizer(fv.toString(), "\t");
				if(!tokens.nextToken().equals(sem.getAttribute()))
					temp.add(fv);
		 	}
		 	if(match!= null)
		 		temp.add(new FStructureElementString(match));
		 	values = temp;
	  }

	  public boolean compareSemAttribute(SemEntry sem)
	  {
	  		String att = sem.getAttribute();
		 	Iterator i = values.iterator();
		 	while(i.hasNext())
		 	{
		 		FStructureElementValue fv = (FStructureElementValue)i.next();
				StringTokenizer tokens = new StringTokenizer(fv.toString(), "\t");
				if(tokens.nextToken().equals(att))
				{
					return true;
				}
		 	}
		 	return false;
	  }

	public String toString()
	{
		return "";
	}

	public String toXML()
	{
		return "";
	}

	public boolean equals(Object obj)
	{
		return true;
	}

	public boolean merge(FStructureInterface f)
	{
		return true;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		System.out.println("cannot change name of this object");
	}

	public Object clone()
	{
		return null;
	}

	public void addElement(FStructureElementValue value) {
		// TODO Auto-generated method stub

	}

	public String[] getFeatures() {
		// TODO Auto-generated method stub
		return null;
	}

	public HashSet getFeaturesAlias() {
		// TODO Auto-generated method stub
		return null;
	}
}